package com.oa.lms.apps.dao.user.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.user.ISectionInfoDao;
import com.oa.lms.apps.entity.user.SectionInfoValue;

public class SectionInfoDaoImpl extends HibernateDaoSupport implements
		ISectionInfoDao {

	List<SectionInfoValue> list;
	
	public Integer add(SectionInfoValue sectionInfoValue) {
		if (sectionInfoValue != null) {
			this.getHibernateTemplate().save(sectionInfoValue);
		}
		return sectionInfoValue.getSectionId();
	}

	@Override
	public void modify(SectionInfoValue sectionInfoValue) {
		if (sectionInfoValue != null) {
			this.getHibernateTemplate().update(sectionInfoValue);
		}
	}

	@Override
	public SectionInfoValue queryBySectionId(Integer sectionId) {
		if (sectionId != null) {
			return (SectionInfoValue) this.getHibernateTemplate().get(SectionInfoValue.class, sectionId);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SectionInfoValue> querySectionInfo(final SectionInfoValue sectionInfoValue) {
		String hqlStr = "FROM SectionInfoValue sv WHERE 1=1";
		if (sectionInfoValue.getSectionName() != null) {
			hqlStr += " AND sv.sectionName = ?";
		}
		if (sectionInfoValue.getStatus() != null){
			hqlStr += " AND sv.status = ?";
		}
		hqlStr += " AND sv.status <> -1";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (sectionInfoValue.getSectionName() != null) {
					query.setString(++index, sectionInfoValue.getSectionName());
				}
				if (sectionInfoValue.getStatus() != null){
					query.setInteger(++index, sectionInfoValue.getStatus());
				}
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<SectionInfoValue> getList() {
		return list;
	}

	public void setList(List<SectionInfoValue> list) {
		this.list = list;
	}
	
	
	
}
